package com.titaniue.java1;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: JavaSenior
 * @description: 实现TCP网络编程
 * <p>
 * 案例2 ：实现客户端发送文件给服务端，服务端将文件保存到本地
 * @author: renfei.huang
 * @create: 2021-10-24 10:37
 */
public class TCPTest2 {


    @Test
    public void client() throws IOException {

        Socket socke = new Socket(InetAddress.getByName("127.0.0.1"), 9090);

        OutputStream outputStream = socke.getOutputStream();

        FileInputStream fileInputStream = new FileInputStream(new File("zzz.jpg"));

        byte[] buffer = new byte[1024];
        int len;
        while ((len = fileInputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (socke != null) {
            try {
                socke.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    public void server() throws IOException {

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1.
            serverSocket = new ServerSocket(9090);
            //2.
            socket = serverSocket.accept();
            //3.
            inputStream = socket.getInputStream();
            //4.
            fileOutputStream = new FileOutputStream(new File("zzz1.jpg"));

            //5.
            byte[] buffer = new byte[1024];
            int len;
            while((len = inputStream.read(buffer)) != -1){
                fileOutputStream.write(buffer,0,len);
            }
            System.out.println("接收图片成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


        }


    }
}
